﻿namespace Jhong.FunMVCBlog.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Entity;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class HomeController : Controller
    {
        public static int Count = 10;

        private BlogSysData _sysData = new BlogSysData();

        private ArticlesData _articlesData = new ArticlesData();

        private LinksData _linkData = new LinksData();

        private CategoryData _categoryData = new CategoryData();

        [OutputCache(500)]
        public ActionResult Index(int? id)
        {
            this.SetLayoutData();

            var data = this._articlesData.GetIndexPage(id == null ? 1 : id.Value, Count);

            var d2 = new List<Articles>();
            for (int i = 0; i < Math.Min(Count, data.Count); i++) d2.Add(data[i]);

            ViewData["data"] = d2;
            ViewData["page"] = GetPage(data.Count(), null == id ? 1 : id.Value);
            return View();
        }

        public void SetLayoutData()
        {
            ViewData["blogName"] = this._sysData.GetBlogName();
            ViewData["nickName"] = this._sysData.GetNickName();
            ViewData["personThumbnail"] = this._sysData.GetPersonThumbnail();
            ViewData["homepage"] = this._sysData.GetHomePage();
            ViewData["about"] = this._sysData.GetAbout();
            ViewData["lastpublish"] = this._articlesData.GetLastArticles(10);
            ViewData["link"] = this._linkData.GetAll();
            ViewData["category"] = this._categoryData.Cache;
        }

        [OutputCache(1000)]
        public ActionResult Article(Guid id)
        {
            this.SetLayoutData();
            var article = this._articlesData.GetByID(id);
            ViewData["model"] = article;
            return View();
        }

        [OutputCache(500)]
        public ActionResult Category(Guid id, int? id2)
        {
            this.SetLayoutData();

            ViewData["nav"] = " <ol class='breadcrumb'><li><i class='glyphicon glyphicon-home'></i> <a href='/'>首页</a></li><li class='active'><i class='glyphicon glyphicon-file'></i> " + GetCategoryName(id) + "</li></ol>";

            var data = this._articlesData.GetIndexPage(id2 == null ? 1 : id2.Value, Count, id);

            var d2 = new List<Articles>();
            for (int i = 0; i < Math.Min(Count, data.Count); i++) d2.Add(data[i]);

            ViewData["data"] = d2;
            ViewData["page"] = GetPage(data.Count(), null == id2 ? 1 : id2.Value);
            return View("Index");
        }

        public static string GetCategoryName(Guid id)
        {
            return new CategoryData().Cache[id];
        }

        public static string GetTagName(Guid articleID)
        {
            var tgData = new TagData();
            var tags = tgData.GetByArticleID(articleID);
            var tgsb = new StringBuilder();
            tgsb.Append("<div class='panel-footer'>");
            tgsb.Append("<i class='glyphicon glyphicon-tags'></i>");
            foreach (var tag in tags)
            {
                tgsb.Append(string.Format("[ <a href='###'>{0}</a> ]", tgData.Cache[tag.TagID]));
            }
            tgsb.Append("</div>");
            return tgsb.ToString();
        }

        private string GetPage(int totalCount, int page)
        {
            var sb = new StringBuilder();
            sb.Append("<ul class='pager'>");
            if (page > 1)
            {
                sb.Append(" </li><li class='previous'><a href='/Home/Index/" + (page - 1).ToString() + "' title='前一页'><i class='glyphicon glyphicon-arrow-left'></i></a></li>");
            }
            if (totalCount > Count)
            {
                sb.Append(" </li><li class='next'><a href='/Home/Index/" + (page + 1).ToString() + "' title='下一页'><i class='glyphicon glyphicon-arrow-right'></i></a></li>");
            }
            sb.Append("</ul>");
            return sb.ToString();
        }
    }
}